<?php
use yii\helpers\Html;
use yii\helpers\Url;
use backend\widgets\GridView;
use yii\widgets\Breadcrumbs;
use backend\models\User;
use backend\models\service\AdminService as Admin;
use backend\models\service\NovelService as Novel;
use backend\models\service\Chapter;
$this->title = '订单列表';
?>

<div class="RightCenter clear">
    <div class="RightTime">
        <div class="Time">
            <h3>订单列表</h3>
        </div>
    </div>
    <div class="Read_date" style="margin-top:20px;margin-bottom: 5px;height:60px;">
        <ul>
            <li class="Read_LisJ ">
                <div class="Seek">
                    <input class="form-control AppIN" id="names" placeholder="请输入用户账号" type="text" value="<?php echo isset($select['Order']['phone']) ? $select['Order']['phone'] : '';?>">
                    <input class="form-control AppIN" id="titles" placeholder="请输入书籍名称" type="text" value="<?php echo isset($select['Order']['novel_title']) ? $select['Order']['novel_title'] : '';?>">
                    <button class="btn btn-primary SS" type="button">搜索</button>
                </div>
                <div class="SX clear Read_DdGL">
                    筛选:
                    <select id="main">
                        <option value="">全部频道</option>
                        <option <?php echo isset($select['Order']['main']) && $select['Order']['main'] == 1 ? 'selected="selected"' : '';?> value="1">男生频道</option>
                        <option <?php echo isset($select['Order']['main']) && $select['Order']['main'] == 2 ? 'selected="selected"' : '';?> value="2">女生频道</option>
                        <option <?php echo isset($select['Order']['main']) && $select['Order']['main'] == '0' ? 'selected="selected"' : '';?> value="0">未分频道</option>
                    </select>

                    <select class="cate">
                        <option value="">全部分类</option>
                        <?php foreach ($category as $cate): ?>
                            <option <?php echo isset($select['Order']['category']) && $select['Order']['category'] == $cate['cate_id'] ? 'selected="selected"' : '';?> value="<?php echo $cate['cate_id']; ?>"><?php echo $cate['cate_name']; ?></option>
                        <?php endforeach; ?>
                    </select>
                    <select id="cate" style="display:none;">
                        <option value="">全部分类</option>
                    </select>
                    <select id="status">
                        <option value="">全部订单状态</option>
                        <option <?php echo isset($select['Order']['status']) && $select['Order']['status'] == 1 ? 'selected="selected"' : '';?> value="1">成功</option>
                        <option <?php echo isset($select['Order']['status']) && $select['Order']['status'] == '0' ? 'selected="selected"' : '';?> value="0">失败</option>
                    </select>
                    <button class="btn btn-primary czS" type="button">重置</button>
                </div>
            </li>
        </ul>
    </div>
    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'id' => 'grid',
        'columns' => [
            [
                'attribute' => 'id',
                'format' => 'raw',
                'contentOptions' => ['class' => 'TR_Tows'],
                'label' => '订单号',
                'headerOptions' => ['width' => 10],
            ],
            [
                'label' => '书籍名称',
                'headerOptions' => ['width' => 30],
                'value' => function($data){
                    $model = new Novel();
                    $res = $model->getNovelInfo($data->novel_id);
                    return $res['novel_title'];
                }
            ],
            [
                'attribute' => 'novel_chapter',
                'label' => '所购章节',
                'headerOptions' => ['width' => 20],
                'value' => function($data){
                    $model = new Chapter();
                    $res = $model->getChaptersInfo($data->novel_chapter);
                    return $res['chapter_name'];
                }
            ],
             [
                'attribute' => 'pay_coin',
                'label' => '阅币消费',
                'headerOptions' => ['width' => 10],
            ],
            [
//                'attribute' => 'create_time',
                'label' => '昵称账号',
                'headerOptions' => ['width' => 30],
                'value' => function($data){
                    $model = new User();
                    $res = $model->getUserInfo($data->user_id);
                    return $res['user_nickname'];
                }
            ],
                    [
                'format' => 'raw',
                'contentOptions' => ['class' => 'TR_Tows'],
                'label' => '注册来源',
                'headerOptions' => ['width' => 50],
                'value' => function($data){
                    $model = new User();
                    $res = $model->getUserInfo($data->user_id);
                    $channel_id = $res['channel_id'];
                    $partner_id = $res['partner_id'];
                    $admin = new Admin();
                    $cInfos = $admin->getInfoDatas($channel_id);
                    $pInfos = $admin->getInfoDatas($partner_id);
                    return $cInfos['username'] ."</br>".$pInfos['username'];
                }
            ],
            [
                'attribute' => 'status',
                'label' => '订单状态',
                'headerOptions' => ['width' => 20],
                'value' => function($data) {
                    switch ($data->status){
                        case 0:
                            return '失败';
                        case 1:
                            return '成功';
                    }
                }
            ],
        ],
    ]); ?>
</div>
<script>
    $(function(){
        var main = $("#main").val();
        if(main == ''){
            $(".cate").show();
            $("#cate").hide();
        }else{
            $(".cate").hide();
            getCate(main);
        }
    });
    
    $("#main").change(function(){
        var main = $("#main").val();
        if(main == ''){
            $(".cate").show();
            $("#cate").hide();
        }else{
            $(".cate").hide();
            getCate(main);
        }
    });
    var cid = '<?php echo isset($select['Order']['category']) ? $select['Order']['category'] : '';?>';
    function getCate(main){
        $("#cate").show();
        $.ajax({
            url:"/novel/ajax-get-category",
            type:"post",
            dataType:"json",
            data:{main:main},
            success:function(data){
                if(data.status == 0){
                    var str = "<option value=''>全部分类</option>";
                    var list = data.list;
                    for(i in list){
                        str += "<option value='"+list[i]['cate_id']+"'>"+list[i]['cate_name']+"</option>";
                    }
                    $("#cate").empty();
                    $("#cate").append(str);
                    $("#cate").find("option[value='"+cid+"']").attr('selected','selected')
                }
            }
        });
    }
    
    $(".SS").click(function(){
        var phone = $("#names").val();
        var title = $("#titles").val();
        //频道
        var main = $("#main").val();
        //分类
        var cate = $("#cate").val();
        if(!cate){
            cate = $(".cate").val();
        }
        var status = $("#status").val();
        location.href="/order/index?Order[novel_title]="+title+"&Order[phone]="+phone+"&Order[main]="+main+"&Order[category]="+cate+"&Order[status]="+status
    });
    
    $(".czS").click(function(){
        location.href="/order/index"
    });
</script>